const loginUrl = 'http://10.49.41.13:8080/apps/opp/index.html?cacheVersion=ZCOP1.3.3.2'
const USER_CODE = '625533'
const USER_PASSWORD = 'ztzq@123'

const {Builder, By, Key, until} = require('selenium-webdriver');
// const {Options} = require('selenium-webdriver/chrome');

// const options = new Options();

let driver = new Builder().forBrowser('chrome').build();


async function getYanZhengMa(phone='13888888888'){
    let driver2 = new Builder().forBrowser('chrome').build();
    await driver2.get(loginUrl);
    await driver2.sleep(1300);
    //1.点击弹框的确定
    await driver2.findElement(By.css('.messager-button .btn-text')).click();
    //2.自动填充账号密码,并且回车
    await driver2.findElement(By.css('#USER_CODE')).sendKeys('10056')
    await driver2.findElement(By.css('#TRD_PWD')).sendKeys(USER_PASSWORD, Key.RETURN);
    await driver2.sleep(2300);
    //3.点击业务管理
    await driver2.findElement(By.css('.nav-tabs-wrap .tabs-wrap ul li:nth-of-type(2)')).click();
    await driver2.sleep(300);

    //4.点击电信验证查询打雷
    await driver2.findElement(By.css('div[node-id="99990100"] span.tree-icon')).click();
    await driver2.sleep(300);
    //5.点击短信验证码小类
    await driver2.findElement(By.css('div[node-id="99990102"] span.tree-icon')).click();
    await driver2.sleep(2300);
    //6.跳到iframe里
    await driver2.switchTo().frame(driver2.findElement(By.css(".layout-panel-center div.panel[style*='display: block'] iframe")));
    await driver2.sleep(2300);
    console.log(1111111111111111111111111111111111111111111,`td[title=${phone}] + td`)
    //7.查找验证码
    let a = await driver2.findElement(By.css(`td[title='${phone}'] + td`)).getAttribute('title')
    console.log(a)
    driver2.close();
    return a
};

(async function start() {
    //1.打开登陆页
    await driver.get(loginUrl);
    await driver.sleep(1300);
    //点击弹框的确定
    await driver.findElement(By.css('.messager-button .btn-text')).click();
    //2.自动填充账号密码,并且回车
    await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    await driver.findElement(By.css('#TRD_PWD')).sendKeys(USER_PASSWORD, Key.RETURN);

    
    //点击弹框
    await driver.sleep(2000);
    //await driver.findElement(By.css('.panel-body .dialog-button div:nth-of-type(2) span.btn-text')).click();
    await driver.switchTo().defaultContent();
    await driver.sleep(2000);
    try {
        await driver.findElement(By.css(".busi-track-body .dialog-button .l-btn-wrap:nth-of-type(2) a .btn-text")).click();
      } catch (error) {
        console.error('点击失败:', error);
      }

    //3.跳到主页后点击业务受理
    await driver.sleep(2000);
    await driver.findElement(By.css('#counter')).click();
    await driver.sleep(2000);
    //跳转IFRAME
    await driver.switchTo().frame(driver.findElement(By.css('iframe[src="http://10.49.41.13:8080/apps/opp/operator/pages/cust-recognite-web2.html?cacheVersion=ZCOP1.3.3.2_upd001&noscroll=true&YZT_CHANNEL=1"]')));
    //4.点击个人开户
    await driver.sleep(2000);
    try {
       // await driver.findElement(By.css('#counter')).click();
        await driver.findElement(By.css('#custOpenAcctBtn .open-account-btn-span')).click();
    } catch (error) {
        console.error('点击个人开户按钮失败:', error);
    }
    

    await driver.sleep(2000);

    //跳出iframe
    await driver.switchTo().defaultContent();
    await driver.sleep(2000);
    await driver.switchTo().frame(driver.findElement(By.css('iframe[src="../operator/open-account/open-account.html?BUSI_SCOPE=2&BUSI_CODE=QS035&USER_TYPE=0&isNewBSnoFlag=1&theme=web2&ORG_CODE=0062&isNav=false&cacheVersion=ZCOP1.3.3.2_upd001&noscroll=false"]')));


    // //5.录入表单
    await driver.findElement(By.css('#CUST_FNAME')).sendKeys("liuyj测试个人03", Key.RETURN)
    await driver.sleep(100);
    await driver.findElement(By.css('.messager-window .messager-input.validatebox-text.validatebox-must')).sendKeys('liuyj测试个人03', Key.RETURN)
    await driver.sleep(100);
    await driver.findElement(By.css("input[comboname='ID_TYPE'] + span .validatebox-text")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div[panel-name='ID_TYPE'] div[value='00']")).click()
    await driver.sleep(100);
    let idcard = generateIDCard('370783','19980524')
    await driver.findElement(By.css('#ID_CODE')).sendKeys(idcard, Key.RETURN)
    await driver.sleep(100);
    await driver.findElement(By.css('.messager-window .messager-input.validatebox-text.validatebox-must')).sendKeys(idcard, Key.RETURN)
    await driver.sleep(100);
    await driver.findElement(By.css('#ID_BEG_DATE + span .combo-text')).sendKeys("20000101", Key.RETURN)
    await driver.sleep(100);
    await driver.findElement(By.css('#ID_EXP_DATE ~ div.obviousbox .item-checkbox')).click()
    await driver.sleep(100);
    await driver.findElement(By.css('#ID_ADDR')).sendKeys("测试证件地址")
    await driver.sleep(100);
    await driver.findElement(By.css('#ID_ISS_AGCY')).sendKeys("测试发证机关")
    await driver.sleep(100);
    await driver.findElement(By.css('#NATIVE_PLACE')).sendKeys("山东")
    await driver.sleep(100);
    await driver.findElement(By.css("input[comboname='EDUCATION'] + span .validatebox-text")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div[panel-name='EDUCATION'] div[value='2']")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("input[comboname='OCCU_TYPE'] + span .validatebox-text")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div[panel-name='OCCU_TYPE'] div[value='22']")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("input[comboname='OCCU_STATUS'] + span .validatebox-text")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div[panel-name='OCCU_STATUS'] div[value='1']")).click()
    await driver.sleep(100);
    await driver.findElement(By.css('#WORKPLACE')).sendKeys("山东蓝翔")
    await driver.sleep(100);
    await driver.findElement(By.css("input[comboname='POSITION'] + span .validatebox-text")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div[panel-name='POSITION'] div[value='2']")).click()
    let number = generatePhoneNumber()
    await driver.sleep(100);
    await driver.findElement(By.css('#MOBILE_TEL')).sendKeys(number)
    await driver.sleep(100);
    await driver.findElement(By.css('#TEL + span.teltextarea-wrap input[maxlength="5"]')).sendKeys('123')
    await driver.sleep(100);
    await driver.findElement(By.css('#TEL + span.teltextarea-wrap input[maxlength="32"]')).sendKeys('123123132')
    await driver.sleep(100);
    await driver.findElement(By.css('#EMAIL')).sendKeys('123123132@qq.com')
    await driver.sleep(100);
    await driver.findElement(By.css('#FAX + span.teltextarea-wrap input[maxlength="5"]')).sendKeys('123')
    await driver.sleep(100);
    await driver.findElement(By.css('#FAX + span.teltextarea-wrap input[maxlength="32"]')).sendKeys('123123132')

    await driver.sleep(100);
    await driver.findElement(By.css("#ADDRESS + span>span:nth-of-type(1)")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div.combo-panel[style*='display: block'] div.panel-body div[value='山东省']")).click()

    await driver.sleep(100);
    await driver.findElement(By.css("#ADDRESS + span>span:nth-of-type(2)")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div.combo-panel[style*='display: block'] div.panel-body div[value='济南市']")).click()

    await driver.sleep(100);
    await driver.findElement(By.css("#ADDRESS + span>span:nth-of-type(3)")).click()
    await driver.sleep(100);
    await driver.findElement(By.css("div.combo-panel[style*='display: block'] div.panel-body div[value='市中区']")).click()

    await driver.sleep(100);
    await driver.findElement(By.css("#ADDRESS + span>input:nth-of-type(4)")).sendKeys('泉城街道')

    //点击发送验证码按钮
    await driver.findElement(By.css("#linkInfoForm .input-btn")).click()
    await driver.sleep(100);
    await driver.findElement(By.css(".messager-body .btn-text")).click()
    let numberRes = await getYanZhengMa(number)
    await driver.sleep(100);
    await driver.findElement(By.css("#VALIDATE_CODE")).sendKeys(numberRes)

    await driver.sleep(3300);
    console.log("点击了下一步事件~~~~~~~~~~~~~~~~~~")
    await driver.findElement(By.css("#next")).click()
    await driver.findElement(By.css("#next")).click()
    console.log("点击了下一步事件2~~~~~~~~~~~~~~~~~~")
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    // await driver.findElement(By.css('#USER_CODE')).sendKeys(USER_CODE)
    //3.
    // let renjiComplted = false
    // let condition = async()=>driver.wait(until.elementTextContains(await driver.findElement(By.id("lg_tbar")),'刘乙江'),300);
    // while (!renjiComplted) {
    //     await driver.sleep(3000);//给用户三秒
    //     //刷新界面
    //     await condition().then(res=>{
    //         renjiComplted = true
    //     }).catch(()=>{
    //         renjiComplted = false
    //     })
    // }
    // await driver.sleep(2000);
    // //此时已经登陆进入页面了
    // await driver.findElement(By.css("a[data-lg-tj-id='4s00']")).click();//点击招聘按钮
    // await driver.sleep(2000);
    // await driver.findElement(By.css('#keyword')).sendKeys('前端', Key.RETURN);
    // await driver.sleep(2000);
    // await getData()
    // console.log("result::::::::::::",result)
})();




function generateIDCard(){
    var coefficientArray = [ "7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2"];// 加权因子
    var lastNumberArray = [ "1","0","X","9","8","7","6","5","4","3","2"];// 校验码
    var address = "420101"; // 住址
    var birthday = "19810101"; // 生日
    var s = Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString() + Math.floor(Math.random()*10).toString();
    var array = (address + birthday + s).split(""); 
    var total = 0;
    for(i in array)
    {
        total = total + parseInt(array[i])*parseInt(coefficientArray[i]);
    } 
    var lastNumber = lastNumberArray[parseInt(total%11)];
    var id_no_String = address + birthday + s + lastNumber;
    //var x = document.getElementsByName("id_no");
    var x="420101198101010000";
    for (var i = 0; i < x.length; i++) 
    {
        var o = x[i];
        o.value = id_no_String;
    }
    return(id_no_String);
}
function generatePhoneNumber() {
    const prefixes = ['13', '14', '15', '16', '17', '18', '19'];
    const prefix = prefixes[Math.floor(Math.random() * prefixes.length)];
    let body = '';
    for (let i = 0; i < 9; i++) {
      body += Math.floor(Math.random() * 10);
    }
    return prefix + body;
  }